package weaver.limitCurrent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;

public class PropUtil {
  private static final Logger LOGGER = Logger.getLogger(PropUtil.class);
  
  public static Properties getProperties(File file) {
    Properties properties = new Properties();
    InputStream io = null;
    try {
      io = new FileInputStream(file);
      properties.load(io);
      io.close();
    } catch (FileNotFoundException ignored) {
      LOGGER.error("properties file not found");
    } catch (IOException e) {
      LOGGER.error(e.getClass().getName(), e);
    } finally {
      if (io != null)
        try {
          io.close();
        } catch (IOException e) {
          LOGGER.error("close stream Exception", e);
        }  
    } 
    return properties;
  }
  
  public static void propTransIntoMap(Map<String, String> initMap, Properties prop) {
    Set<Object> keySet = prop.keySet();
    for (Object key : keySet) {
      String propKey = ((String)key).trim().toLowerCase();
      String propValue = prop.getProperty((String)key).trim()
        .toLowerCase();
      if (initMap.containsKey(propKey))
        LOGGER.error(String.valueOf(propKey) + 
            "is duplicate,place check your properties"); 
      initMap.put(propKey, propValue);
    } 
  }
}
